package be.dioubs.sportTrack.adapter;

import android.content.Context;
import android.database.Cursor;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
import be.dioubs.sportTrack.R;
import be.dioubs.sportTrack.common.Constant;
import be.dioubs.sportTrack.common.GlobalFormatter;
import be.dioubs.sportTrack.dto.TrackDTO;

public class TracksAdapter extends SimpleCursorAdapter {
    private Cursor _cursor;
    private Context _context;

    Drawable drawableRun;
    Drawable drawableBike;
    Drawable drawableSwim;    
    
    public TracksAdapter(Context context, int layout, Cursor c, String[] from, int[] to) {
        super(context, layout, c, from, to);
        _cursor = c;
        _context = context;

        drawableRun = _context.getResources().getDrawable(R.drawable.run);
        drawableBike = _context.getResources().getDrawable(R.drawable.bike);
        drawableSwim = _context.getResources().getDrawable(R.drawable.swim);    
    }
 
//    @Override
//    public View newView(Context context, Cursor cursor, ViewGroup parent) {
//    	//super.newView(context, cursor, parent);
//    	
//    	//LinearLayout listActions = (LinearLayout) parent.findViewById(R.id.eventListActions);
//    	//listActions.setVisibility(View.GONE);
//    	
//		return parent;
//    }
    
    @Override
    public void bindView(View view, Context context, Cursor cursor) {
    	super.bindView(view, context, cursor);    	
    	
    	Object tag = view.getTag();
    	if(tag != null)
    	{
        	LinearLayout listActions = (LinearLayout) view.findViewById(R.id.eventListActions);
        	listActions.setVisibility(View.GONE);
    	}
    	else
    		view.setTag("TO BE RECYCLED");
    	
    	// Image
        ImageView imageView = (ImageView) view.findViewById(R.id.image);
 
        int keySport = _cursor.getColumnIndex(TrackDTO.KEY_SPORT);
        String sport = _cursor.getString(keySport);

        if(sport.equals(Constant.SPORT_RUN))
        	imageView.setImageDrawable(drawableRun);
        else if(sport.equals(Constant.SPORT_BIKE))
        	imageView.setImageDrawable(drawableBike);
        else
        	imageView.setImageDrawable(drawableSwim);

        // Date
        TextView dateView = (TextView) view.findViewById(R.id.date);
        
        int keyDate = _cursor.getColumnIndex(TrackDTO.KEY_DATE);
        Long dateTime = _cursor.getLong(keyDate);
        
        dateView.setText(GlobalFormatter.date(dateTime));
		
		// Title
        TextView titleView = (TextView) view.findViewById(R.id.title);
        
        int keyTitle = _cursor.getColumnIndex(TrackDTO.KEY_TITLE);
        String title = _cursor.getString(keyTitle);
        
        // If title's length exceeds 20 chars, constraining it to 20 and add "...".
        if(title.length() > 20)
        	title = title.substring(0, 20) + "...";
        
        titleView.setText(title);
    }
}
